<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>

<script>
    // new
    function myNew(Func, ...args) {
        const obj = {};
        obj.__proto__ = Func.prototype;
        let result = Func.apply(obj, args);
        return result instanceof Object ? result : obj;
    }

    // instanceof
    function newInstanceOf(leftValue, rightValue) {
        let rightProto = rightValue.prototype;
        leftValue = leftValue.__proto__;
        while (true) {
            if (leftValue === null)
                return false;
            if (leftValue === rightProto)
                return true;
            leftValue = leftValue.__proto__;
        }
    }

    // console.log(newInstanceOf([], Array));

    // 节流
    function throttled(fn, delay) {
        let timer = null;
        let startTime = Date.now();
        return function() {
            let curTime = Date.now();
            let remaining = delay - (curTime - startTime);
            let args = arguments;
            let context =this;
            clearTimeout(timer);
            if (remaining <= 0) {
                fn.apply(context, args);
                startTime = Date.now();
            } else {
                timer = setTimeout(fn, remaining);
            }
        }
    }

    let promiseALL = function (promiseArr) {
        let arrlen = promiseArr.length;
        let cnt = 0;
        let resArr = new Array(arrlen);

        return new Promise((resolve, reject) => {
            promiseArr.forEach((promise, index) => {
                promise.then(res =>{
                    resArr[index] = res;
                    cnt++;
                    if (cnt === arrlen)
                        resolve(resArr);
                }, err => {
                    reject(err);
                })
            });
        })
    }

    let promiseAll = function (promiseArr) {
        let len = promiseArr.length;
        let res = new Array(len);
        let cnt = 0;

        return new Promise((resolve, reject) => {
            promiseArr.forEach((promise, index) => {
                promise.then(res => {
                    resArr[index] = res;
                    cnt++;
                    if (cnt === len) {
                        resolve(resArr);
                    }
                }, err => {
                    reject(err)
                })
            })
        })
    }



</script>

</html>